import hashlib
import datetime


def generateUserToken(username):
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    token_data = f"{username}{current_time}"
    hashed_token = hashlib.sha256(token_data.encode()).hexdigest()
    return hashed_token


def compareUserTokens(db, userid, token):
    try:
        query = "SELECT token FROM user WHERE userid = %s"
        data = (userid,)
        with db.cursor() as cursor:
            cursor.execute(query, data)
            data = cursor.fetchall()
        return True if data and data[0][0] == token else False
    except Exception as e:
        print(e)
        return False


